package cn.itcast.web.exception;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *  统一异常处理
 *      异常处理器
 *          实现HandlerExceptionResolver
 */

//@Component
public class ConsumerExceptionResolver implements HandlerExceptionResolver{
    /*
        进行异常处理
            1.跳转页面
            2.携带信息
     */
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        //1.判断异常类型
        if(ex instanceof UnauthorizedException){
            //如果是权限不正常：UnauthorizedException
            ModelAndView mv = new ModelAndView();
            mv.setViewName("forward:/unauthorized.jsp");
            return mv;
        }else {
            //其他异常
            ModelAndView mv = new ModelAndView();
            //错误信息
            mv.addObject("errorMsg", "出错了");
            mv.addObject("ex", ex);
            //错误页面
            mv.setViewName("error");
            return mv;
        }
    }
}
